﻿using System;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormNhomThueHangHoaThem : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormNhomThueHangHoaThem()
        {
            InitializeComponent();
        }
        private void FormNhomThueHangHoaThem_Load(object sender, EventArgs e)
        {
            Reset();
        }

        private void txtTenNhomThue_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void cbbLoaiThue_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void bttLuu_Click(object sender, EventArgs e)
        {
            Create();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void Reset()
        {
            txtDienGiai.Text = "";
            txtTenNhomThue.Text = "";
            bttLuu.Enabled = false;
            cbbLoaiThue.Items.Insert(0,"Chọn loại thuế...");
            cbbLoaiThue.Items.Insert(1, "Thuế xuất");
            cbbLoaiThue.Items.Insert(2, "Thuế nhập");
            cbbLoaiThue.SelectedIndex = 0;
        }

        void KiemTra()
        {
            if (txtTenNhomThue.Text!="" && cbbLoaiThue.SelectedIndex!=0)
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        void Create()
        {
            try
            {
                var t = new NhomThueHangHoa();
                t.TenThue = txtTenNhomThue.Text;
                if (cbbLoaiThue.SelectedIndex==1)
                {
                    t.LoaiThue = 0;// thuế xuất
                }
                else
                {
                    t.LoaiThue = 1;// thue nhap
                }
                
                t.DienGiai = txtDienGiai.Text;
                db.NhomThueHangHoas.Add(t);
                db.SaveChanges();
                MessageBox.Show("Thêm mới thành công!");
                Reset();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


    }
}
